package com.zz.tijian.entity.res;
import cn.hutool.http.HttpStatus;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel(value = "统一响应格式")
public class Result<T> {

    public static final String REQUEST_OK = "OK";

    @ApiModelProperty(value = "业务状态码，200-成功，其它-失败",required = true,example = "200")
    private int code;
    @ApiModelProperty(value = "响应消息", example = "OK")
    private String msg;
    @ApiModelProperty(value = "响应数据",example = "data")
    private T data;


    public static Result<Void> ok() {
        return new Result<Void>(HttpStatus.HTTP_OK, REQUEST_OK, null);
    }

    public static <T> Result<T> ok(T data) {
        return new Result<>(HttpStatus.HTTP_OK, REQUEST_OK, data);
    }

    public static <T> Result<T> error(String msg) {
        return new Result<>(HttpStatus.HTTP_BAD_REQUEST, msg, null);
    }
    public static String errorOfString(){
        String result= "{\"code\":\"400\",\"msg\":\"调用方法超过最大次数，限流返回！\"}";
        Result<Object> result1 = new Result<>();
        result1.setCode(400);
        result1.setMsg("当前访问人数过多，请稍后再试！");
        return result1.toString();
    }
    public static <T> Result<T> error(int code, String msg) {
        return new Result<>(code, msg, null);
    }

    public Result() {
    }

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public boolean success() {
        return code == HttpStatus.HTTP_OK;
    }
}
